<template>
  <div class="login-view">
    <div class="container">
      <div class="title">
        <h2>光谷智慧交通管理平台</h2>
        <img src="../../assets/image/login2.png" alt="" />
      </div>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="60px"
        class="demo-ruleForm"
        label-position="top"
      >
        <h3 class="title">注册账号</h3>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            autocomplete="off"
            placeholder="6位，由数字组成"
            show-password
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPassword">
          <el-input
            v-model="ruleForm.checkPassword"
            type="password"
            autocomplete="off"
            placeholder="6位，由数字组成"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            注册
          </el-button>
          <!-- <el-button @click="resetForm(ruleFormRef)">重置</el-button> -->
          <el-button text @click="jumpLogin()"> 返回登录 </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ElMessage, ElMessageBox } from "element-plus";
import { UserHttp } from "../../request/UserHttp.js";
const $router = useRouter();
// 表单dom 表单数据
const ruleFormRef = ref();
const ruleForm = reactive({
  username: "",
  password: "",
  type: "common",
  checkPassword: "",
});

// 表单规则
const checkName = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入用户名"));
  } else {
    callback();
  }
};
const validatePass = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    // 设置正则表达式 密码为6位数字
    let reg = /\d{6}/;
    let res = reg.test(value);
    if (!res) {
      callback(new Error("请输入6位数字"));
    } else {
      callback();
    }
  }
};
const validatePass2 = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请再次输入密码"));
  } else if (value !== ruleForm.password) {
    callback(new Error("两次输入不一致!"));
  } else {
    callback();
  }
};
const rules = reactive({
  username: [{ validator: checkName, trigger: "blur" }],
  password: [{ validator: validatePass, trigger: "blur" }],
  checkPassword: [{ validator: validatePass2, trigger: "blur" }],
});

// 提交/重置表单
const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (valid) {
      // 表单验证 valid 有效
      console.log("提交成功!");
      // 进行注册  成功即返回登录

      let resRegister = await UserHttp.RegisterUserHttp(ruleForm);
      console.log(resRegister, 777);
      //   注册成功 默认用户类型为common
      if (resRegister.data.code === 200) {
        // ElMessage({
        //   message: "注册成功",
        //   type: "success",
        // });
        // 是否返回登录页
        ElMessageBox.confirm("注册成功，是否返回登录页?", "注册成功", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "success",
        })
          .then(() => {
            setTimeout(() => {
              $router.push("/login");
            }, 500);
          })
          .catch(() => {});
      } else {
        // 注册失败
        ElMessage.error("用户名重复,请重新注册");
      }
    } else {
      // 表单验证 valid 无效
      console.log("提交失败!");
      return false;
    }
  });
};

// 返回登录
const jumpLogin = () => {
  $router.push("/login");
};
</script>

<style lang="scss" scoped>
.login-view {
  position: relative;
  width: 100vw;
  height: 100vh;
  background-color: #def5fe;
  background-image: url("../../assets/image/login1.jfif");
  background-size: cover;
  .container {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    gap: 150px;
    .title {
      text-align: center;
      h2 {
        padding-block: 20px;
      }
      img {
        width: 390px;
      }
    }
  }
  .el-form {
    width: 350px;
    padding: 20px 40px;
    background-color: #fff;
    border: 1px #eee solid;
    border-radius: 10px;
    .title {
      padding-bottom: 20px;
      text-align: center;
    }
    .el-button {
      width: 100%;
      margin-bottom: 10px;
    }
    .el-button + .el-button {
      margin-left: 0;
    }
  }
}
</style>
